//
//  CourseDetailsHandler.m
//  mUBB
//
//  Created by Cosmin Stirbu on 1/17/13.
//  Copyright (c) 2013 Cosmin Stirbu. All rights reserved.
//

#import "CourseDetailsHandler.h"
#import "CoursesProtocol.h"
#import "Teacher.h"
#import "StudyGroup.h"
#import "TypeOfClass.h"
#import "Faculty.h"


@implementation CourseDetailsHandler

@synthesize course;

- (id)initWithDelegate:(id)_delegate course:(Course *)_course ticket:(Ticket *)_ticket {
    self = [super initWithDelegate:_delegate ticket:_ticket];
    [self setCourse:_course];
    return self;
}

- (void)handleSuccess:(WSResponse *)response {
    NSDictionary *courseDictionary = [response.data objectFromJSONData];
    course.faculty = [Faculty new];
    
    course.courseCode = [courseDictionary getJSONObjectForKey:COURSE_CODE_TAG];
    course.courseName = [courseDictionary getJSONObjectForKey:COURSE_NAME_TAG];
    course.selfUrl = [courseDictionary getJSONObjectForKey:SELF_URL_TAG];
    course.courseUrl = [courseDictionary getJSONObjectForKey:COURSE_URL_TAG];

    NSDictionary *facultyDictionary = [courseDictionary getJSONObjectForKey:FACULTY_TAG];
    course.faculty.facultyId = [[facultyDictionary getJSONObjectForKey:FACULTY_ID_TAG] integerValue];
    course.faculty.facultyName = [facultyDictionary getJSONObjectForKey:FACULTY_NAME_TAG];
    course.faculty.facultyUrl = [facultyDictionary getJSONObjectForKey:FACULTY_URL_TAG];
    
    course.availableRoomsUrl = [courseDictionary getJSONObjectForKey:AVAILABLE_ROOMS_URL_TAG];
    course.bookingsUrl = [courseDictionary getJSONObjectForKey:BOOKINGS_URL_TAG];
    
    NSMutableArray *teachers = [NSMutableArray new];
    for (NSDictionary *teacherDictionary in [courseDictionary getJSONObjectForKey:TEACHERS_TAG]) {
        Teacher *teacher = [Teacher new];
        teacher.teacherId = [[teacherDictionary getJSONObjectForKey:TEACHER_ID_TAG] integerValue];
        teacher.firstName = [teacherDictionary getJSONObjectForKey:TEACHER_FIRST_NAME_TAG];
        teacher.lastName = [teacherDictionary getJSONObjectForKey:TEACHER_LAST_NAME_TAG];
        teacher.position = [teacherDictionary getJSONObjectForKey:TEACHER_POSITION_TAG];
        teacher.phone = [teacherDictionary getJSONObjectForKey:TEACHER_PHONE_TAG];
        teacher.teacherUrl = [teacherDictionary getJSONObjectForKey:TEACHER_URL_TAG];
        teacher.email = [teacherDictionary getJSONObjectForKey:TEACHER_EMAIL_TAG];
        [teachers addObject:teacher];
    }
    course.teachers = teachers;
    
    NSMutableArray *studyGroups = [NSMutableArray new];
    for (NSDictionary *studyGroupDict in [courseDictionary getJSONObjectForKey:STUDY_GROUPS_TAG]) {
        StudyGroup *studyGroup = [StudyGroup new];
        studyGroup.studyGroupId = [[studyGroupDict getJSONObjectForKey:STUDY_GROUP_ID_TAG] integerValue];
        studyGroup.name = [studyGroupDict getJSONObjectForKey:STUDY_GROUP_NAME_TAG];
        [studyGroups addObject:studyGroup];
    }
    course.studyGroups = studyGroups;
    
    NSMutableArray *typesOfClasses = [NSMutableArray new];
    for (NSDictionary *typeOfClassDict in [courseDictionary getJSONObjectForKey:TYPES_OF_CLASSES_TAG]) {
        TypeOfClass *typeOfClass = [TypeOfClass new];
        typeOfClass.typeOfClassId = [[typeOfClassDict getJSONObjectForKey:TYPE_OF_CLASS_ID_TAG] integerValue];
        typeOfClass.name = [typeOfClassDict getJSONObjectForKey:TYPE_OF_CLASS_NAME_TAG];
        [typesOfClasses addObject:typeOfClass];
    }
    course.typesOfClasses = typesOfClasses;
    
    [self.delegate didReceiveCourseDetailsWithSuccess:course forTicket:self.ticket];
}

- (void)handleError:(WSError *)error {
    [self.delegate didReceiveCourseDetailsWithFailure:error forTicket:self.ticket];
}
@end
